package com.example.john.zyfypt_408lwn.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.example.john.zyfypt_408lwn.R;
import com.example.john.zyfypt_408lwn.fragment.Fragment1;
import com.example.john.zyfypt_408lwn.fragment.Fragment2;
import com.example.john.zyfypt_408lwn.fragment.Fragment3;
import com.example.john.zyfypt_408lwn.fragment.Fragment6;
import com.example.john.zyfypt_408lwn.fragment.Fragment7;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private LinearLayout  layout1,layout2,layout3,layout4,layout6;//鐢ㄤ簬鐣岄潰鍒囨崲
    private ImageButton ib1,ib2,ib3,ib4,ib6;//鐢ㄦ埛璁剧疆鍥剧墖
    private ViewPager vp;//鐢ㄤ簬婊戝姩鍒囨崲

    private List<Fragment> listfragment;//Fragment  list
    private FragmentPagerAdapter fpadapter;//鐢ㄦ潵鍛堢幇Fragment椤甸潰
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();//鍒濆鍖栫晫闈㈡帶浠讹紝璁剧疆鎺т欢鐩戝惉
        initFragment();//鍒濆鍖朏ragment鐩稿叧
    }
    //鍒濆鍖朏ragment鐩稿叧
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();//瀹炰緥鍖朏ragment list
        //灏咶ragment鍔犲叆鍒發ist涓?
        listfragment.add(new Fragment1());
        listfragment.add(new Fragment2());
        listfragment.add(new Fragment3());
        //listfragment.add(new Fragment4());
        //listfragment.add(new Fragment5());
        listfragment.add(new Fragment7());
        listfragment.add(new Fragment6());


        //瀹炰緥鍖朏ragmentPagerAdapter
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            //鑾峰彇鏌愪釜Fragment
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
            //纭畾Fragment鏁伴噺
            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp=(ViewPager)findViewById(R.id.viewPager);//瀹炰緥鍖朧iewPager
        vp.setAdapter(fpadapter);//缁戝畾ViewPager鍜孉dapter
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //鏍规嵁ViewPager鐨刾osition閫夋嫨鏌怓ragment
            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                vp.setCurrentItem(position);
                resetImage();//搴曢儴鍥剧墖閲嶇疆
                selectTab(position);//閫夋嫨鏌愪釜Fragment
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    //layout鐐瑰嚮浜嬩欢
    View.OnClickListener  onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //鍏堝皢鍥涗釜鎸夐挳閮借鎴愭湭閫夊浘鐗?
            resetImage();
            //鐒跺悗鏍规嵁搴曢儴鐨勯€夋嫨鍒囨崲Fragment鍜孷iewPager
            switch (view.getId())
            {
                case R.id.layout1:
                    System.out.println("鎸変笅1");
                    Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍廰rticle", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
                    System.out.println("鎸変笅2");
                    Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍廲ourse", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
                    System.out.println("鎸変笅3");
                    Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍弙ideo", Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
                    System.out.println("鎸変笅4");
                    Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍廲ase", Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
                //case R.id.layout5:
                //System.out.println("鎸変笅5");
                //Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍弍roject", Toast.LENGTH_SHORT).show();
                //selectTab(4);
                //break;
                case R.id.layout6:
                    System.out.println("鎸変笅6");
                    Toast.makeText(MainActivity.this, "鍒囨崲搴曢儴鏍弇e", Toast.LENGTH_SHORT).show();
                    selectTab(5);
                    break;

            }
        }
    };

    private void init() {
        //瀹炰緥鍖朓mageButton涓轰簡鍒囨崲鍥剧墖
        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);
        //ib5=(ImageButton)findViewById(R.id.imageButton5);
        ib6=(ImageButton)findViewById(R.id.imageButton);

        //瀹炰緥鍖朙ayout涓轰簡搴曢儴鏍忕偣鍑讳簨浠跺垏鎹㈤〉闈?
        layout1=(LinearLayout) findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        //layout5=(LinearLayout)findViewById(R.id.layout5);
        layout6=(LinearLayout)findViewById(R.id.layout6);

        //layout鐐瑰嚮浜嬩欢
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        //layout5.setOnClickListener(onClickListener);
        layout6.setOnClickListener(onClickListener);
    }
    //閲嶇疆搴曢儴鏍忓浘鐗囦负鏈€?
    private  void resetImage()
    {
        ib1.setImageResource(R.drawable.article_normal);
        ib2.setImageResource(R.drawable.course_normal);
        ib3.setImageResource(R.drawable.video_normal);
        ib4.setImageResource(R.drawable.case_normal);
        //ib5.setImageResource(R.drawable.project_normal);
        ib6.setImageResource(R.drawable.me_normal);

    }
    //鍒囨崲Fragment鍜孷iewPager
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                ib1.setImageResource(R.drawable.article_pressed);
                break;
            case 1:
                ib2.setImageResource(R.drawable.course_pressed);
                break;
            case 2:
                ib3.setImageResource(R.drawable.video_pressed);
                break;
            case 3:
                ib4.setImageResource(R.drawable.case_pressed);
                break;
            //case 4:
            //ib5.setImageResource(R.drawable.project_pressed);
            //break;
            case 5:
                ib6.setImageResource(R.drawable.me_pressed);
                break;
        }
        vp.setCurrentItem(i);
    }


}